package io.gitee.yxsnake.framework.security.core.service;

import cn.dev33.satoken.stp.StpInterface;
import io.gitee.yxsnake.framework.core.domain.model.LoginUser;
import io.gitee.yxsnake.framework.core.enums.UserType;
import io.gitee.yxsnake.framework.core.exception.ServiceException;
import io.gitee.yxsnake.framework.core.utils.login.LoginHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author snake
 * @description sa-token 权限管理实现类
 * @since 2024/11/20 10:23
 */
public class SaPermissionImpl implements StpInterface {

    /**
     * 获取菜单权限列表
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(Objects.isNull(loginUser)){
            throw new ServiceException("用户信息为空");
        }        UserType userType = UserType.getInstance(loginUser.getUserType());
        if (userType == UserType.SYS_USER) {
            return new ArrayList<>(loginUser.getMenuPermission());
        } else if (userType == UserType.APP_USER) {
            // 其他端 自行根据业务编写
        }
        return new ArrayList<>();
    }

    /**
     * 获取角色权限列表
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(Objects.isNull(loginUser)){
            throw new ServiceException("用户信息为空");
        }
        UserType userType = UserType.getInstance(loginUser.getUserType());
        if (userType == UserType.SYS_USER) {
            return new ArrayList<>(loginUser.getRolePermission());
        } else if (userType == UserType.APP_USER) {
            // 其他端 自行根据业务编写
        }
        return new ArrayList<>();
    }
}


